package com.abc.controller;


import com.abc.entity.User;
import com.abc.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID获取用户", notes = "传入用户的ID获取用户信息")
    public User getUserById(@PathVariable Integer id) {
        return userService.getById(id);
    }

    @PostMapping
    @ApiOperation(value = "添加用户", notes = "传入用户的详细信息进行添加")
    public boolean addUser(@RequestBody User user) {
        // 查看是否存在相同的用户名
        User user1 = userService.getOne(new LambdaQueryWrapper<>(User.class).eq(User::getUserName, user.getUserName()));
        if (user1 != null) {
            return false;
        }
        return userService.save(user);
    }

    @PutMapping
    @ApiOperation(value = "更新用户", notes = "传入用户的详细信息进行更新")
    public boolean updateUser(@RequestBody User user) {
        return userService.updateById(user);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除用户", notes = "传入用户的ID进行删除")
    public boolean deleteUser(@PathVariable Integer id) {
        return userService.removeById(id);
    }
}
